<?php
namespace app\front\controller\v1;
use app\front\controller\v1\BaseController;
use app\front\model\Store;
use app\front\model\Trade;
class ShowController extends BaseController
{
    
    public function test(){
        $host = "r-2ze9c46547a98a34.redis.rds.aliyuncs.com";
        $port = 6379;
        session_start();
        /* 这里替换为实例id和实例password */
        $user = "r-2ze9c46547a98a34";
        $pwd = "zfshlmredis098ABC";
        
        $redis = new \Redis();
        if ($redis->connect($host, $port) == false) {
            die($redis->getLastError());
        }
        /* user:password 拼接成AUTH的密码 */
        if ($redis->auth($user . ":" . $pwd) == false) {
            die($redis->getLastError());
        }
        $redis->set('key','sunliang1');
       echo $redis->get('key');
    }
    
    public function PersonalCenter(){
        return view('v1/qianbao');
    }
    
   public function showRule(){
      return view('v1/rule');
   }
   
   
   public function showTodayOrder(){
       $res= $this->getOrderService()->showTodayOrder();
       $todayMoneySum=0;
       foreach( $res as $v )
       {
           $todayMoneySum= bcadd($todayMoneySum,$v['money'],2);
       }
       logs('time.txt', $res);
       $this->assign('res', $res);
       $this->assign('todayMoneySum', $todayMoneySum);
       //带着参数跳转到gzhPay.html
       return view('v1/todayOrder');
   }
   
   public function showStore(){
       //防止影响其他公众号 $list=$store->where('ISNULL(status)')->limit(2)->select();
       session('openid',null);
       $list=Store::where("status is NULL")-> limit(2)-> select();  // -> order("id DESC")
       $theme=Trade::where("status is NULL")-> select();  // -> order("id DESC")
       $this->assign('themelist',$theme);
       $this->assign('list',$list);
       $this->assign('lock',0);
       return view('v1/index');
   }
   
   //访问商城主页
   public function showStoreForTrade(){
       //防止影响其他公众号
       session('openid',null);
       $trade_id= input('param.trade_id');
       $trade=Trade::where('id='.$trade_id)->find();
       $trade_name=$trade['trade_name'];
       $sign=$trade['sign_imgurl']; 
       $list=Store::where('status is NULL and trade_id='.$trade_id)->select();
       $theme=Trade::where('status is NULL')->select();
       $this->assign('trade_name',$trade_name);
       $this->assign('sign',$sign);
       $this->assign('themelist',$theme);
       $this->assign('list',$list);
       $this->assign('lock',1);
       return view('v1/index2');
   }
   
   public function zhifw(){
       $id=input('param.id');
       $date=Store::where('id='.$id)->find();
       $this->assign('date', $date);
       return view('v1/zhifu');  
   }
   
   public function ajaxShowStore(){
       $keyword = input('param.keyword');
       $where['store_name']=array('like','%'.$keyword.'%');//写查询条件
       $list=  Store::where($where)->select();
       echo json_encode($list);
   }
   
   public function distance($dis){
       $store_data = Store::table('store')->select();
       /* 这里替换为连接的实例host和port */
       $host = "r-2ze9c46547a98a34.redis.rds.aliyuncs.com";
       $port = 6379;
       session_start();
       /* 这里替换为实例id和实例password */
       $user = "r-2ze9c46547a98a34";
       $pwd = "zfshlmredis098ABC";
       
       $redis = new \Redis();
       if ($redis->connect($host, $port) == false) {
           die($redis->getLastError());
       }
       /* user:password 拼接成AUTH的密码 */
       if ($redis->auth($user . ":" . $pwd) == false) {
           die($redis->getLastError());
       }
       if(!$redis->exists("stores".session_id())){
           $store_data = Store::table('store')->select();
           for($i = 0;$i<count($store_data);$i++){
               $url = "http://restapi.amap.com/v3/distance?origins=".$dis."&destination=".$store_data[$i]['position']."&output=json&key=42862ccd797b1c7d11aa3dc8ba58d4b4";
               $ch = curl_init();
               curl_setopt($ch,CURLOPT_URL,$url);
               curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
               curl_setopt($ch,CURLOPT_HEADER,0);
               $result = curl_exec($ch);
               curl_close($ch);
               $store_data[$i]['distance'] = json_decode($result,true)['results'][0]['distance'];
           }
           $distance = array();
           foreach ($store_data as $store) {
               $distance[] = $store['distance'];
           }       
          array_multisort($distance, SORT_ASC, $store_data);
           for($i = 0;$i<count($store_data);$i++){
               $redis->rPush("stores".session_id(),json_encode($store_data[$i]));
           }
           $redis->setTimeout("stores".session_id(),600);
       }
      echo json_encode($redis->lrange("stores".session_id(),0,-1));
   }
   
   public function QueryZhiFu(){
       $store_name = input('param.store_name');
       $store_name='"'.$store_name.'"';
       $store= Store::where('store_name='.$store_name)->field('id')->find();
      $date=Store::where('id='.$store['id'])->find();
      $this->assign('date', $date);
      return view('v1/zhifu');  
   
   }
   
   public function showSuccess(){
       return view('v1/success');
   }
   
}
